package com.ruoyi.business.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.business.domain.Customer;
import com.ruoyi.business.mapper.CustomerMapper;
import com.ruoyi.business.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户信息 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class CustomerServiceImpl implements ICustomerService
{
    @Autowired
    private CustomerMapper customerMapper;


    /**
     * 查询客户信息集合, 高级查询
     * 
     * @param customer 客户信息
     * @return 客户信息集合
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer)
    {
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 查询所有客户
     * 
     * @return 客户列表
     */
    @Override
    public List<Customer> selectAll()
    {
        return customerMapper.selectAll();
    }


    /**
     * 通过客户ID查询客户信息
     * 
     * @param customerId 客户ID
     * @return 角色对象信息
     */
    @Override
    public Customer selectCustomerById(Long customerId)
    {
        return customerMapper.selectByPrimaryKey(customerId);
    }

    /**
     * 批量删除客户信息
     *
     * @param ids 需要删除的数据ID
     * @throws Exception
     */
    @Override
    public int deleteCustomerByIds(String ids) throws BusinessException
    {
        Long[] customerIds = Convert.toLongArray(ids);

        return customerMapper.deleteCustomerByIds(customerIds);
    }

    /**
     * 新增保存客户信息
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public int insertCustomer(Customer customer)
    {
        return customerMapper.insert(customer);
    }

    /**
     * 修改保存客户信息
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public int updateCustomer(Customer customer)
    {
        return customerMapper.updateByPrimaryKey(customer);
    }



    /**
     * 校验客户名称是否唯一
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public String checkCustomerNameUnique(Customer customer)
    {
        Long customerId = StringUtils.isNull(customer.getId()) ? -1L : customer.getId();
        Customer info = customerMapper.checkCustomerNameUnique(customer.getName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != customerId.longValue())
        {
            return UserConstants.CUSTOMER_NAME_NOT_UNIQUE;
        }
        return UserConstants.CUSTOMER_NAME_UNIQUE;
    }
}
